package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Address;
import com.itheima.service.AdressService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/address")
public class AdressController {
    @Reference
    private AdressService adressService;

    @RequestMapping("/findAll")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = adressService.findAll(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Address address) {
        try {
            adressService.add(address);
            return new Result(true, MessageConstant.QUERY_ADRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ADRESS_FAIL);
        }

    }

    @RequestMapping("/findByString")
    public PageResult findByString(@RequestBody QueryPageBean queryPageBean){
        PageResult byString = adressService.findByString(queryPageBean);
        System.out.println(byString.getRows());
        return byString;


    }

    //查询所地址信息,赋值给tableData2
    @RequestMapping("/findAllll")
    public Result findAllll(){
        try {
            List<Address> list = adressService.findAllll();
            return new Result(true,MessageConstant.QUERY_ADRESS_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }


    }

    @RequestMapping("/findadressname")
    public Result findAdressName() {
        try {
            List<String> list = adressService.findAdressName();
            return new Result(true, MessageConstant.QUERY_ADRESS_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ADRESS_FAIL);
        }

    }

    // 删除
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            adressService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ADRESS_FAIL);
        }

        return new Result(true,MessageConstant.DELETE_ADRESS_SUCCESS);
    }

}
